<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>最小K个数</title>
  </head>
  <body>
    <script>
      // 设计一个算法，找出数组中最小的k个数。以任意顺序返回这k个数均可。
      // 示例：
      // 输入： arr = [1,3,5,7,2,4,6,8], k = 4
      // 输出： [1,2,3,4]
      var smallestK = function (arr, k) {
        arr.sort(function (a, b) {
          return a - b;
        });
        return arr.slice(0, k);
      };
      var arr = [1, 3, 5, 7, 2, 4, 6, 8];
      console.log(smallestK(arr, 4));
    </script>
  </body>
</html>
